package com.pctx.bibatracker.service.impl;

import com.pctx.bibacommon.result.Result;
import com.pctx.bibatracker.dao.PositionMapper;
import com.pctx.bibatracker.pojo.Position;
import com.pctx.bibatracker.service.NavigationService;
import com.pctx.bibatracker.util.CoordinateTransforToGaoDe;
import com.pctx.bibatracker.util.CoordinateTransform;
import com.pctx.bibatracker.util.MathUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 导航
 */
@Service("/navigationService")
public class NavigationServiceImpl implements NavigationService {
    @Autowired
    private PositionMapper positionMapper;

    /**
     * 百度导航
     *
     * @param address
     * @param deviceNo
     * @return
     * @throws Exception
     */
    @Override
    public Result selectRouteFromBaiDu(String address, String deviceNo) throws Exception {
        Position position = positionMapper.selectByDeviceUuid(deviceNo);
        String cord = CoordinateTransform.getCoordinate(position.getCordX(), position.getCordY());
        JSONObject jsonObjectone = JSONObject.fromObject(cord);
        JSONArray jsonArrays = jsonObjectone.getJSONArray("result");
        ArrayList list = new ArrayList();
        Map map = new HashMap();
        ArrayList lists = new ArrayList();
        Map maps=new HashMap();
        Object[] os = jsonArrays.toArray();
        for (int i = 0; i < os.length; i++) {
            JSONObject jsonObj = JSONObject.fromObject(os[i]);
            maps.put("x",jsonObj.get("x"));
            maps.put("y",jsonObj.get("y"));
            /*list.add(jsonObj.get("x"));
            list.add(jsonObj.get("y"));
            list.add(jsonObj.toString());*/
        }
        BigDecimal wgs_x =(new BigDecimal(maps.get("x").toString())).setScale(12, BigDecimal.ROUND_HALF_UP);
        BigDecimal wgs_y =(new BigDecimal(maps.get("y").toString())).setScale(12, BigDecimal.ROUND_HALF_UP);
        System.out.println(wgs_x + "_0000" + wgs_y);
        String cordone = CoordinateTransform.tansferAddress(address);
        //地址转换成百度坐标；
        System.out.println(cordone + "{}{}{");
        JSONObject jsonObject = JSONObject.fromObject(cordone);
        JSONObject result = jsonObject.getJSONObject("result");
        JSONObject resultone = result.getJSONObject("location");
        BigDecimal cordx = new BigDecimal(resultone.getString("lng"));
        BigDecimal cordy = new BigDecimal(resultone.getString("lat"));
        //System.out.println(cordx + "++++" + cordy);
        String cordTwo = CoordinateTransform.getNavigation(cordy, cordx, wgs_y, wgs_x);
       // System.out.println(cordTwo + "============");
        JSONObject jsonObjectTwo = JSONObject.fromObject(cordTwo);
        JSONObject resultOne = jsonObjectTwo.getJSONObject("result");
      //  System.out.println(resultOne + "============");
        JSONArray resultTwo = resultOne.getJSONArray("routes");
        //System.out.println(resultTwo.size() + "======09090909");
        Object[] osOne = resultTwo.toArray();
        for (int i = 0; i < osOne.length; i++) {
            JSONObject jsonObj = JSONObject.fromObject(osOne[i]);
            Object jsonObjTwo = jsonObj.get("distance");
            Object jsonObjone = jsonObj.get("duration");
           // System.out.println(jsonObjone);
            JSONArray resultThree = jsonObj.getJSONArray("steps");
           // System.out.println(resultThree);
            Object[] osTwo = resultThree.toArray();
            JSONArray jsonObjOne = JSONArray.fromObject(osTwo);
            map.put("duration", jsonObjone);
            map.put("step", jsonObjOne);
            map.put("distance", jsonObjTwo);
            lists.add(map);
        }
        return Result.success(lists, "成功");
    }

    /**
     * 高德导航
     *
     * @param address
     * @param deviceNo
     * @return
     * @throws Exception
     */
    @Override
    public Result selectRouteFromGaoDe(String address, String deviceNo) throws Exception {
        Position position = positionMapper.selectByDeviceUuid(deviceNo);
        String cord = CoordinateTransforToGaoDe.getCoordinate(position.getCordX(), position.getCordY());
        System.out.println(cord);
        JSONObject jsonObject = JSONObject.fromObject(cord);
        String[] h = jsonObject.get("locations").toString().split(",");
        ArrayList list = new ArrayList();
        for (int i = 0; i < h.length; i++) {
            list.add(h[i]);
        }
        BigDecimal wgs_x = MathUtils.getBigDecimal(list.get(0)).setScale(12, BigDecimal.ROUND_HALF_UP);
        BigDecimal wgs_y = MathUtils.getBigDecimal(list.get(1)).setScale(12, BigDecimal.ROUND_HALF_UP);
        System.out.println(wgs_x + "," + wgs_y);
        String cordone = CoordinateTransforToGaoDe.tansferAddress(address);
        JSONObject jsonObjectOne = JSONObject.fromObject(cordone);
        JSONArray result = jsonObjectOne.getJSONArray("geocodes");
        ArrayList lists = new ArrayList();
        Object[] os = result.toArray();
        for (int i = 0; i < os.length; i++) {
            JSONObject jsonObj = JSONObject.fromObject(os[i]);
            String b = jsonObj.get("location").toString();
            System.out.println(b + "----11111111");
            String[] c = b.split(",");
            for (int j = 0; j < c.length; j++) {
                lists.add(c[0]);
                lists.add(c[1]);
            }
        }
        BigDecimal x = MathUtils.getBigDecimal(lists.get(0)).setScale(12, BigDecimal.ROUND_HALF_UP);
        BigDecimal y = MathUtils.getBigDecimal(lists.get(1)).setScale(12, BigDecimal.ROUND_HALF_UP);
        System.out.println(x + "," + y);
        String cordTwo = CoordinateTransforToGaoDe.getNavigation(x, y, wgs_x, wgs_y);
        System.out.println(cordTwo);
        JSONObject jsonObjectTwo = JSONObject.fromObject(cordTwo);
        JSONObject resultOne = jsonObjectTwo.getJSONObject("route");
        Object json = resultOne.get("paths");
        return Result.success(json, "成功");
    }
}
